package memory.net;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import memory.*;




@SuppressWarnings("serial")
public class ServerWindow extends JFrame implements ActionListener {
	
	memory.net.ConnectClients connector;
	
	private Timer checkClientsTimer = new Timer(2000,this);
	
	//Settings
	private JPanel settingsPanel = new JPanel();
	private JLabel settingsLabel = new JLabel("Settings");
	private JLabel gameNameLabel = new JLabel("Game name");
	private JTextField portField = new JTextField(5);
	private JLabel portLabel = new JLabel("Port");
	private JTextField gameNameField = new JTextField("My game",15);
	
	private JLabel rowsLabel = new JLabel("Rows");
	private JLabel colsLabel = new JLabel("   Cols");
	private SpinnerNumberModel rowsMod = new SpinnerNumberModel(Options.getRows(),1,10,1);
	private SpinnerNumberModel colsMod = new SpinnerNumberModel(Options.getCols(),1,10,1);
	private JSpinner rowsSpinner = new JSpinner(rowsMod);
	private JSpinner colsSpinner = new JSpinner(colsMod);
	
	
	//ButtonsArea
	private JPanel buttonsPanel = new JPanel();
	private JButton startServerButton = new JButton("Start server");
	private JButton cancelButton = new JButton("Cancel");
		
	public MainWindow mwRef;
	
	public ServerWindow(memory.MainWindow mw) {
		
		mwRef = mw;
		setLocationRelativeTo(mwRef);
		mwRef.setEnabled(false);
		
		checkClientsTimer.setRepeats(false);
		checkClientsTimer.addActionListener(this);
		
		setIconImage(Toolkit.getDefaultToolkit().getImage("./data/1.png"));
		
		this.setLayout(new GridBagLayout());
		settingsLabel.setFont(new Font("Sans Serif",1,18));
		settingsPanel.setLayout(new GridBagLayout());
		buttonsPanel.setLayout(new FlowLayout());
		
		GridBagConstraints frameConstraints = new GridBagConstraints();
		GridBagConstraints settingsConstraints = new GridBagConstraints();
		
		//Add settings
		frameConstraints.gridx = 1;
		add(settingsPanel,frameConstraints);
		
		settingsConstraints.insets = new Insets(10,2,60,10);
		
		settingsConstraints.gridx = 0;
		settingsConstraints.gridy = 0;
		settingsConstraints.gridwidth = 4;
		settingsPanel.add(settingsLabel,settingsConstraints);
		
		settingsConstraints.insets = new Insets(10,2,10,10);
		
		settingsConstraints.gridx = 0;
		settingsConstraints.gridy = 1;
		settingsConstraints.gridwidth = 2;
		settingsPanel.add(gameNameLabel,settingsConstraints);
		
		settingsConstraints.gridx = 2;
		settingsConstraints.gridy = 1;
		settingsConstraints.gridwidth = 2;
		settingsPanel.add(gameNameField,settingsConstraints);
		
		settingsConstraints.gridx = 0;
		settingsConstraints.gridy = 2;
		settingsConstraints.gridwidth = 2;
		settingsPanel.add(portLabel,settingsConstraints);
		
		settingsConstraints.gridx = 2;
		settingsConstraints.gridy = 2;
		settingsConstraints.gridwidth = 2;
		settingsPanel.add(portField,settingsConstraints);
		
		settingsConstraints.insets = new Insets(10,2,55,10);
		
		settingsConstraints.gridx = 0;
		settingsConstraints.gridy = 3;
		settingsConstraints.gridwidth = 1;
		settingsPanel.add(rowsLabel,settingsConstraints);
		
		settingsConstraints.gridx = 1;
		settingsConstraints.gridy = 3;
		settingsConstraints.gridwidth = 1;
		settingsPanel.add(rowsSpinner,settingsConstraints);
		
		settingsConstraints.gridx = 2;
		settingsConstraints.gridy = 3;
		settingsConstraints.gridwidth = 1;
		settingsPanel.add(colsLabel,settingsConstraints);
				
		settingsConstraints.gridx = 3;
		settingsConstraints.gridy = 3;
		settingsConstraints.gridwidth = 1;
		settingsPanel.add(colsSpinner,settingsConstraints);
		
		portField.setText(String.valueOf(Options.getPort()));
		rowsMod.setValue(Options.getRows());
		colsMod.setValue(Options.getCols());
		
		//Add buttons
		frameConstraints.gridx = 0;
		frameConstraints.gridy = 1;
		frameConstraints.gridwidth = 2;
		add(buttonsPanel,frameConstraints);
		buttonsPanel.add(startServerButton);
		buttonsPanel.add(cancelButton);
		
		
		//Add listener
		startServerButton.addActionListener(this);
		cancelButton.addActionListener(this);
		
		//Junk
		pack();
		setVisible(true);
		
	}
	
	//Actions!
	public void actionPerformed(ActionEvent e) {
		
		if (e.getSource() == startServerButton) {
			int r = rowsMod.getNumber().intValue();
			int c = rowsMod.getNumber().intValue();
			int p = Integer.parseInt(portField.getText());
			
			if (gameNameField.getText().length() == 0) {
				JOptionPane.showMessageDialog(this, "Please enter game name!");
			}
			
			else if (p < 1000 || p > 65535) {
				JOptionPane.showMessageDialog(this,"Port setting must be within the range of 1000 to 65535!","Error",JOptionPane.ERROR_MESSAGE);
			}
			
			else if ( (r*c)%2 != 0 ) {
				JOptionPane.showMessageDialog(this, "Number of cards must be even!","Error",JOptionPane.ERROR_MESSAGE);
			}
			
			else if ( (r*c) > Tools.getImgArray().length*2 ) {
				JOptionPane.showMessageDialog(this, "Number of cards exceeding number of images in img-dir!","Error",JOptionPane.ERROR_MESSAGE);
			}
		
			else {
								
				Options.setRows(r);
				Options.setCols(c);
				Options.setPort(p);
				connector = new ConnectClients(gameNameField.getText(),Options.getPort(),new DebugWindow());
				new ClientLobby(this,connector,"ServerPlayer");
				
			}
		}
		
		//Cancel button
		else if (e.getSource() == cancelButton) {
			mwRef.setEnabled(true);
			dispose();
		}
		
	}

	
	
	
	
}


